"""A sector defines a region of space"""

import random
import ctypes
from pyglet.gl import *
from starfield import StarField
from planet import GiantPlanet
import obj
import view
import game

class Sector:
	def __init__(self, position=(0,0)):
		self.position = position
		self.contents = []


class TacticalView(game.State, view.ScrollingView):

	SCROLL_KEY_SPEED = 400
	VIEW_ANGLE = 50 # Angle of view along the x-axis from top-down
	WIDTH = HEIGHT = 3000

	def __init__(self, sector):
		self.sector = sector
		self.stars = StarField(self.WIDTH, self.HEIGHT)
		self.planet = GiantPlanet(400.0, 
			band_deviation=random.uniform(1.5, 2.0), 
			tilt=random.gauss(0, 20),
			color=(0.1, 0.4, 0.7))
		self.station = view.Wireframe(obj.Model('model/sc-station.obj', scale=60.0),
			color=(1.0, 0.9, 0.5))
	
	def enter(self, window):
		glShadeModel(GL_SMOOTH)
		glEnable(GL_LIGHTING)
		glEnable(GL_LIGHT0)
		fourfv = ctypes.c_float * 4
		glLoadIdentity()
		glRotatef(self.VIEW_ANGLE, 1, 0, 0)
		glLightfv(GL_LIGHT0, GL_POSITION, fourfv(-1, 0, 1, 0))
		#glLightfv(GL_LIGHT0, GL_AMBIENT, fourfv(.1, .1, .1, 3))
		glLightfv(GL_LIGHT0, GL_DIFFUSE, fourfv(1, 1, 1, 1))
		#glLightfv(GL_LIGHT0, GL_SPECULAR, fourfv(1, 1, 1, 1))
	
	def on_draw(self):
		game.window.clear()
		glLoadIdentity()
		self.translate_view(scaling=0.4)
		self.stars.draw()
		glLoadIdentity()
		self.translate_view(scaling=0.75)
		glRotatef(self.VIEW_ANGLE, 1, 0, 0)
		self.planet.draw()
		glLoadIdentity()
		self.translate_view()
		glTranslatef(-400, -400, 500)
		glRotatef(self.VIEW_ANGLE, 1, 0, 0)
		glRotatef(self.planet.tilt, 0, 0, 1)
		glRotatef(game.time * 2.5, 0, 1, 0)
		self.station.draw()
